<script>
import {doDelete, doGet, doPost} from "../http/httpRequest.js";
import {messageConfirm, messageTip} from "../util/util.js";

export default {
  name: "ClueView",

  inject: ["reload"],

  data() {
    return {
      //线索列表对象
      clueList: [{
        ownerDO: {},
        activityDO: {},
        appellationDO: {},
        needLoanDO: {},
        intentionStateDO: {},
        intentionProductDO: {},
        stateDO: {},
        sourceDO: {}
      }],
      //每页显示多少条数据
      pageSize: 0,
      //总共查询多少数据
      total: 0,
      //导入excel的弹窗
      importExcelDialogVisible: false,
      //线索id数组
      clueIdArray: []
    }
  },

  methods: {
    //查询线索列表数据
    getData(current) {
      doGet("/api/clues", {
        current: current  //当前页
      }).then(resp => {
        if (resp.data.code == 200) {
          this.clueList = resp.data.data.list;
          this.total = resp.data.data.total;
          this.pageSize = resp.data.data.pageSize;
        }
      })
    },

    //分页函数
    toPage(current) {
      this.getData(current);
    },

    //导入excel
    importExcel() {
      //弹个窗口
      this.importExcelDialogVisible = true;
    },

    //文件上传函数
    uploadFile(param) {
      console.log(param);
      let fileObj = param.file;   //相当于input里取得的files
      let formData = new FormData();
      formData.append('file', fileObj);
      doPost("/api/importExcel", formData).then(resp => {
        if (resp.data.code === 200) {
          //Excel导入成功，提示一下
          messageTip("导入成功", "success");
          //清理一下上传的文件
          this.$refs.uploadRef.clearFiles();
          //页面刷新
          this.reload();
        } else {
          //Excel导入失败
          messageTip("导入失败", "error");
        }
      })
    },

    //提交文件按钮
    submitExcel() {
      this.$refs.uploadRef.submit();
    },

    //添加线索
    addClue() {
      this.$router.push("/dashboard/clue/add")
    },

    //编辑线索
    edit(id) {
      this.$router.push("/dashboard/clue/edit/" + id);
    },

    //线索详情
    view(id) {
      this.$router.push("/dashboard/clue/detail/" + id);
    },

    //删除线索
    del(id) {
      messageConfirm("确定要删除吗?").then(() => {
        doDelete("/api/clue/" + id, {}).then(resp => {
          if (resp.data.code == 200) {
            messageTip("删除线索成功", "success")
            //实现页面刷新
            this.reload();
          } else {
            messageTip("删除线索失败，原因: " + "该线索已经存在相关数据", "error")
          }
        })
      }).catch(() => {
        messageTip("取消删除线索", "warning")
      })
    },

    //勾选复选框触发函数
    handleSelectionChange(selectionDataArray) {
      console.log(selectionDataArray);
      this.clueIdArray = []
      selectionDataArray.forEach(data => {
        let clueId = data.id
        this.clueIdArray.push(clueId);
      })
    },

    //批量删除市场活动
    batchDelClue() {
      if (this.clueIdArray.length <= 0) {
        messageTip("请选择要删除的数据", "warning")
        return;
      }
      messageConfirm("确定要删除这些数据吗?").then(() => {
        //数组变为字符串
        let ids = this.clueIdArray.join(",")
        doDelete("/api/clue", {
          ids: ids
        }).then(resp => {
          if (resp.data.code == 200) {
            messageTip("批量删除线索成功", "success")
            //实现页面刷新
            this.reload();
          } else {
            messageTip("批量删除线索失败，原因: " + resp.data.msg, "error")
          }
        })
      }).catch(() => {
        messageTip("取消批量删除线索", "warning")
      })
    }
  },

  mounted() {
    this.getData(1);
  }

}
</script>

<template>

  <!--三个按钮-->
  <el-button type="primary" class="btn" @click="addClue()" v-has-permission="'clue:add'">录入线索</el-button>
  <el-button type="success" class="btn" @click="importExcel()" v-has-permission="'clue:import'">导入线索(Excel)
  </el-button>
  <el-button type="danger" class="btn" @click="batchDelClue" v-has-permission="'clue:delete'">批量删除</el-button>

  <!--数据展示-->
  <el-table
      :data="clueList"
      style="width: 100%"
      @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="50"/>
    <el-table-column type="index" label="序号" width="65"/>
    <el-table-column property="ownerDO.name" label="负责人" width="120"/>
    <el-table-column property="activityDO.name" label="所属活动"/>
    <el-table-column label="姓名">
      <template #default="scope">
        <a href="javascript:" @click="view(scope.row.id)">{{ scope.row.fullName }}</a>
      </template>
    </el-table-column>
    <el-table-column property="appellationDO.typeValue" label="称呼"/>
    <el-table-column property="phone" label="手机" width="120"/>
    <el-table-column property="weixin" label="微信" width="120"/>
    <el-table-column property="needLoanDO.typeValue" label="是否贷款"/>
    <el-table-column property="intentionStateDO.typeValue" label="意向状态"/>
    <el-table-column property="intentionProductDO.name" label="意向产品"/>

    <el-table-column label="线索状态">
      <template #default="scope">
        <span style="color: #67c23a" v-if="scope.row.state==-1"> {{ scope.row.stateDO.typeValue }}</span>
        <span v-else>{{ scope.row.stateDO.typeValue }}</span>
      </template>
    </el-table-column>

    <el-table-column property="sourceDO.typeValue" label="线索来源"/>
    <el-table-column property="nextContactTime" label="下次联系时间" width="165"/>
    <el-table-column label="操作" width="230">
      <template #default="scope">
        <el-button type="primary" @click="view(scope.row.id)" v-has-permission="'clue:view'">详情</el-button>
        <el-button type="success" @click="edit(scope.row.id)" v-hasPermission="'clue:edit'">编辑</el-button>
        <el-button type="danger" @click="del(scope.row.id)" v-has-permission="'clue:delete'">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!--分页导航-->
  <p>
    <el-pagination
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :total="total"
        @prev-click="toPage"
        @next-click="toPage"
        @current-change="toPage"/>
  </p>

  <!--excel导入弹窗-->
  <el-dialog v-model="importExcelDialogVisible" title="导入线索excel" width="55%" center draggable>

    <el-upload
        ref="uploadRef"
        :http-request="uploadFile"
        :auto-upload="false"
    >
      <template #trigger>
        <el-button type="primary">选择Excel文件</el-button>
      </template>
      仅支持后缀名为.xls和.xlsx的文件

      <template #tip>
        <div class="fileTip">
          重要提示：
          <ul>
            <li>上传仅支持后缀名为.xls或.xlsx的文件；</li>
            <li>给定Excel文件的第一行将视为字段名；</li>
            <li>请确认您的文件大小不超过50MB；</li>
            <li>日期值以文本形式保存，必须符合yyyy-MM-dd格式；</li>
            <li>日期时间以文本形式保存，必须符合yyyy-MM-dd HH:mm:ss的格式；</li>
          </ul>
        </div>
      </template>

    </el-upload>


    <template #footer>
      <div class="dialog-footer">
        <el-button @click="importExcelDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="submitExcel()">导 入</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.el-table {
  margin-top: 15px;
}

.fileTip {
  padding-top: 15px;
}
</style>